Skip to content

feat(sigsafe): allocate safely inside intercepted libc calls - #596

Open
wan9chi wants to merge 4 commits into
mainfrom
claude/fspy-libc-async-signal-safe-129134
Open

feat(sigsafe): allocate safely inside intercepted libc calls#596
wan9chi wants to merge 4 commits into
mainfrom
claude/fspy-libc-async-signal-safe-129134

Conversation

@wan9chi

@wan9chi wan9chi commented Aug 9, 2026

Copy link
Copy Markdown
Member

Part of #605 — this lands the malloc-class fix (the largest of the hazards there); the lazy-dlsym, hot-path panic, TLS reentrancy, and posix_spawn-thread items remain follow-ups.

The benchmark suite that prices this change merged in #602.

Motivation

The preload library runs inside libc calls such as open, stat, and execve. Programs are allowed to make these calls from a signal handler, or in the child of fork() in a program with many threads. In both situations, using libc's malloc can hang the program forever: the lock inside malloc may be held by a thread that is paused or no longer exists. The preload library still allocates through malloc today, so a traced program can hang in exactly these situations.

What this does

Adds a new crate, sigsafe: Unix syscall wrappers that are safe to call where libc is not — in signal handlers, in fork children, before libc has finished initializing. Its README states the three rules everything in it follows: syscalls only (never through libc on Linux), no locks and no hidden state, and no global allocation.

The no-libc rule is enforced at compile time. rustix can be built with a libc backend, and anything in the dependency graph — including crates outside this repository — can select it; no build script can detect the feature-unification case. So sigsafe's lib.rs references rustix::runtime, a module that exists only in rustix's raw-syscall build: selecting the libc backend makes the crate fail to compile instead of silently losing the guarantee.

On top of the first wrappers (mm::mmap_anonymous, mm::munmap, param::page_size) sits sigsafe::alloc, allocation that never touches malloc, in three layers with only the top exposed:

  • MmapAllocator — every allocation asks the kernel for fresh memory pages through sigsafe::mm. It keeps no state of its own, so there is nothing a signal or a fork() can catch locked or half-written.
  • ChunkPool — keeps up to 64 freed 64 KiB chunks in a fixed array of atomic pointers, so the next call can reuse memory without asking the kernel again. Taking or returning a chunk is one atomic swap per slot, never a lock, and a thread that disappears mid-operation can strand at most the one chunk it held.
  • alloc::arena() — the only public function. It hands one intercepted call its own bump arena (a bump_scope::Bump) that draws chunks from the pool and returns them when the call ends. Values allocated in the arena cannot outlive the call; the borrow checker enforces it.

Uses the arena in one place to start: RawExec::to_c_str_array, which builds the NULL-terminated argv/envp pointer arrays that an intercepted exec hands to the real call, then drops them when it returns. That temporary's lifetime is already exactly a bump arena's, so the change is nine lines and adds no unsafe.

It has to come off malloc because exec runs in the child of fork() in multithreaded programs — posix_spawn forks then execs — where malloc's lock may be held by a thread that no longer exists. Both platforms take this path on every intercepted exec.

The strings the array points at are still owned by Exec and still come from malloc, as does the rest of the preload; converting them is follow-up. This change establishes the crate, the layers, and the lifetime discipline in the smallest place all three apply.

Benchmark

The access-relative suite (#602) was added while this PR still used the arena for the fd-relative join, and it earned its keep twice: an early run showed +29% on Linux, which turned out to be the preload building without optimizations (fixed by #597), and the corrected runs showed the arena join costing ~+3% over PathBuf::push — which is why the join reverted and the arena moved to execveat. The investigation is written up in this comment's thread. With the join reverted, both suites should sit at baseline.

Commits

The first commit is an earlier version of the allocator — one lock-free size-class allocator installed as the preload's #[global_allocator] — kept so the two designs can be compared; #599 measured that design end to end and lost. The second commit replaces it with the arena design above. The third moves the allocator into the new sigsafe crate as sigsafe::alloc, adds mm/param and the compile-time backend enforcement, and adds the README. The fourth moves the arena use from the join to execveat and fixes the dangling pointer there.

🤖 Generated with Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@socket-security

socket-security Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedcargo/​bump-scope@​2.3.310010093100100

View full report

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

fspy benchmark

linux

dynamic/launch             change  -0.25%  [ -5.84% ..  +6.69%]  overhead   +60.09%
dynamic/access             change  +0.65%  [ -7.20% .. +30.44%]  overhead   +11.24%
dynamic/access-relative    change  +0.72%  [ -6.13% .. +14.21%]  overhead   +57.09%
static/launch              change  +0.07%  [ -7.93% ..  +9.38%]  overhead  +174.38%
static/access              change  +0.32%  [ -8.39% .. +10.43%]  overhead  +835.99%
static/access-relative     change  +0.00%  [ -2.30% ..  +2.26%]  overhead +1341.46%

macos

dynamic/launch             change  -0.55%  [ -5.13% ..  +4.87%]  overhead  +217.23%
dynamic/access             change  +0.70%  [ -6.16% ..  +8.34%]  overhead    +4.76%
dynamic/access-relative    change  +0.00%  [ -2.89% ..  +3.61%]  overhead  +383.87%

windows

dynamic/launch             change  +0.09%  [ -2.90% ..  +1.81%]  overhead   +28.14%
dynamic/access             change  +0.19%  [ -1.11% ..  +1.11%]  overhead    +0.93%
dynamic/access-relative    change  +0.18%  [ -0.93% ..  +1.10%]  overhead    +1.11%

@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch from 5a2d5e3 to 2e51e33 Compare August 9, 2026 01:00
@wan9chi
wan9chi changed the base branch from main to claude/fspy-artifact-opt-level August 9, 2026 01:00
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch from 2e51e33 to 252bea9 Compare August 9, 2026 01:11
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch from 252bea9 to 4da71f0 Compare August 9, 2026 01:29
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch from 4da71f0 to 81ea504 Compare August 9, 2026 01:33
Base automatically changed from claude/fspy-artifact-opt-level to main August 9, 2026 01:41
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch 2 times, most recently from 3562a1d to 9e93e64 Compare August 9, 2026 01:42
@wan9chi

wan9chi commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

Alternative implementation for comparison: #599 installs a lock-free #[global_allocator] in the preload instead of a per-call arena. Both PRs carry the same access-relative benchmark suite and target main, so their benchmark comments can be read side by side. One of the two should be closed once the numbers land.

@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch from 9e93e64 to 94dbffb Compare August 9, 2026 03:07
@wan9chi
wan9chi changed the base branch from main to claude/fspy-benchmark-access-relative August 9, 2026 03:09
@wan9chi wan9chi changed the title feat(fspy): add an allocator that is safe inside intercepted libc calls feat(sigsafe): allocate safely inside intercepted libc calls Aug 9, 2026
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch 2 times, most recently from 39af197 to 2ff6465 Compare August 9, 2026 03:21
Base automatically changed from claude/fspy-benchmark-access-relative to main August 9, 2026 03:33
wan9chi added a commit that referenced this pull request Aug 9, 2026
## Motivation

The `access` suite opens an absolute path, which the tracker serves with
a borrowed pointer — the code that resolves a file descriptor's
directory and joins it with a relative pathname never runs, so that lane
has been unmeasured. Upcoming changes
([#596](#596)) modify
exactly that lane, so it needs a benchmark row before those land.

## What this does

Adds an `access-relative` suite: the target opens a bare filename with
the filesystem root as its working directory, forcing the
working-directory lookup and the join. Root as the working directory
makes the resolved path byte-identical to the absolute suite's, so the
two rows differ only in the work being priced, not in what gets captured
— and validation asserts the same captured path in both modes.

The launcher grows a `--relative` flag instead of a new binary: the
harness compares two builds of the same launcher source, so one
parameterized binary keeps both suites measured by identical code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch from 2ff6465 to d390c01 Compare August 9, 2026 03:33
wan9chi and others added 2 commits August 9, 2026 11:33
…or the preload library

The preload library interposes libc functions that POSIX declares
async-signal-safe (open, stat, execve, ...), so its Rust allocations must
not take libc malloc's locks: a signal handler, or the child of fork() in
a multithreaded process, would deadlock on locks held by suspended or
vanished threads.

fspy_alloc routes every allocation in the preload cdylib through a
lock-free size-class pool: power-of-two classes carve blocks from 1 MiB
slabs, freed blocks recycle through per-class Treiber free lists made
ABA-resistant by a 40-bit generation tag, and larger or over-aligned
requests map directly. All memory comes from anonymous mappings issued as
raw syscalls via rustix's linux_raw backend — on Linux the allocator
relies on nothing from libc, discovering even the page size with an
mprotect probe (no getauxval, no /proc, no minimum kernel version).
macOS goes through the thin libSystem stubs, its only syscall interface.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…unk pool

Rework fspy_alloc from a size-class global allocator into three layers:
MmapAllocator (stateless, every allocation is a fresh kernel mapping),
ChunkPool (a lock-free cache of 64 KiB chunks), and arena(), the only
public entry, which hands each intercepted call its own bump arena
(bump_scope::Bump) drawing chunks from the process-wide pool.

Use the arena for the first preload call site: joining a directory and a
relative path when resolving fd-relative opens. Add an access-relative
benchmark suite so this lane — working-directory resolution plus path
joining — is measured; the existing absolute-path suite never enters it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch 2 times, most recently from 7a57a18 to 7d5d985 Compare August 9, 2026 03:52
… wrappers

The allocator is the first piece of a broader need: the preload library
runs inside intercepted libc calls, so everything it uses must work in
signal handlers, in the child of fork() in a multithreaded process, and
before libc has finished initializing. sigsafe is where such code now
lives. fspy_alloc becomes sigsafe::alloc, and the mmap/munmap/page-size
calls it makes move behind sigsafe::mm and sigsafe::param, the first
safe syscall wrappers the crate offers on its own.

sigsafe promises that on Linux none of its calls go through libc, and
enforces the promise at compile time: lib.rs references rustix::runtime,
a module that exists only in rustix's raw-syscall backend, so any build
configuration that selects rustix's libc backend — including a feature
enabled outside this repository, which no build script can see — fails
to compile instead of silently keeping libc in the picture. The README
explains the purpose, the rules, and the enforcement.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch 3 times, most recently from 1071b8a to 8a21e7c Compare August 9, 2026 06:32
The first use of the arena: `to_c_str_array` builds the NULL-terminated
argv/envp pointer arrays that an intercepted exec hands to the real
call, then drops them when that call returns — a temporary whose
lifetime is already exactly a bump arena's.

Building it must not go through libc malloc. Exec runs in the child of
fork() in multithreaded programs (posix_spawn forks then execs), where
malloc's lock may be held by a thread that no longer exists. Both
platforms take this path, on every intercepted exec.

The strings the array points at are still owned by `Exec` and still
come from malloc; converting them, and the rest of the preload, is
follow-up work. This change establishes the crate, the layers, and the
lifetime discipline in the smallest place they all apply.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@wan9chi
wan9chi force-pushed the claude/fspy-libc-async-signal-safe-129134 branch from 8a21e7c to aa221e7 Compare August 9, 2026 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant